import java.util.ArrayDeque;
import java.util.Deque;

public class Calculate {
    // 想的太复杂了，没过
    public int calculate1(String s) {
        Deque<Character> op = new ArrayDeque<>();
        Deque<Integer> nums = new ArrayDeque<>();
        char[] ch = s.toCharArray();
        int index = 0;
        while (index < ch.length) {
            if (ch[index] >= '0' && ch[index] <= '9') {
                int num = 0;
                while (index < ch.length && ch[index] >= '0' && ch[index] <= '9') {
                    num = num * 10 + (ch[index++] - '0');
                }
                nums.addLast(num);
                while (nums.size() >= 0 && op.size() != 0 && op.peekLast() != '(') {
                    addOrDevice(op, nums);
                }
            } else if (ch[index] == ' ') {
                index++;
            } else {
                if (ch[index] == ')') {
                    while (nums.size() >= 2 && op.peekLast() != '(') {
                        addOrDevice(op, nums);
                    }
                    index++;
                    op.pollLast();
                } else {
                    op.addLast(ch[index++]);
                }
            }
        }
        while (op.size() != 0 && nums.size() >= 2) {
            addOrDevice(op, nums);
        }
        return nums.pollLast();
    }

    private void addOrDevice(Deque<Character> op, Deque<Integer> nums) {
        int num2 = nums.pollLast();
        int num1 = nums.pollLast();
        char operation = op.pollLast();
        int sum = 0;
        if (operation == '+' ) {
            sum = num1 + num2;
        } else {
            sum = num1 - num2;
        }
        nums.addLast(sum);
    }


    // 基本计算器
    // https://leetcode.cn/problems/basic-calculator/description/
    // 通过sign来记录括号中的正负号
    public int calculate(String s) {
        int ret = 0;
        int sign = 1;
        Deque<Integer> ops = new ArrayDeque<>();
        ops.addLast(sign);
        char[] ch = s.toCharArray();
        int index = 0;
        while (index < ch.length) {
            if (ch[index] == ' ') {
                index++;
            } else if (ch[index] == '+') {
                sign = ops.peekLast();
                index++;
            } else if (ch[index] == '-') {
                sign = -ops.peekLast();
                index++;
            } else if (ch[index] == '(') {
                ops.addLast(sign);
                index++;
            } else if (ch[index] == ')') {
                ops.pollLast();
                index++;
            } else {
                long num = 0l;
                while (index < ch.length && ch[index] >= '0' && ch[index] <= '9') {
                    num = num * 10 + (ch[index++] - '0');
                }
                ret += sign * num;
            }
        }
        return ret;
    }

    public static void main(String[] args) {
//        System.out.println(new Calculate().calculate("(1+(4+5+2)-3)+(6+8)"));
//        System.out.println(new Calculate().calculate(" 2-1 + 2 "));
        System.out.println(new Calculate().calculate("1-(     -2)"));
    }

}
